package com.ruoyi.eqmonitor.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.eqmonitor.domain.OptHandleFlowRecord;
import com.ruoyi.eqmonitor.domain.OptPatrolTask;
import com.ruoyi.eqmonitor.dtos.OptPatrolRecordDto;
import com.ruoyi.eqmonitor.service.IOptHandleFlowRecordService;
import com.ruoyi.eqmonitor.service.IOptPatrolTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.eqmonitor.mapper.OptPatrolRecordMapper;
import com.ruoyi.eqmonitor.domain.OptPatrolRecord;
import com.ruoyi.eqmonitor.service.IOptPatrolRecordService;

/**
 * 巡查记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-05
 */
@Service
public class OptPatrolRecordServiceImpl extends ServiceImpl<OptPatrolRecordMapper,OptPatrolRecord> implements IOptPatrolRecordService {
    
    @Autowired
    private OptPatrolRecordMapper optPatrolRecordMapper;

    @Autowired
    private IOptHandleFlowRecordService optHandleFlowRecordService;

    @Autowired
    private IOptPatrolTaskService optPatrolTaskService;

    @Override
    public OptPatrolRecord selectOptPatrolRecordById(Long id){
        return optPatrolRecordMapper.selectOptPatrolRecordById(id);
    }
      
    @Override
    public List<OptPatrolRecord> selectOptPatrolRecordList(OptPatrolRecord optPatrolRecord){
        return optPatrolRecordMapper.selectOptPatrolRecordList(optPatrolRecord);
    }

    @Override
    public List<Map<String, Object>> patrolStatistics(OptPatrolRecordDto dto) {
        return baseMapper.patrolStatistics(dto);
    }

    @Override
    public boolean add(OptPatrolRecord optPatrolRecord) {
        //按日期生成巡查编号
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String patrolRecordCode = sdf.format(date);
        optPatrolRecord.setCode("XC" + patrolRecordCode);

        optPatrolRecord.setCreateTime(new Date());
        optPatrolRecord.setCreateBy(SecurityUtils.getUsername());
        optPatrolRecord.setCheckUserId(SecurityUtils.getUserId());//当前登录用户Id
        optPatrolRecord.setCheckUserName(SecurityUtils.getUsername());//当前登录用户姓名

        boolean isSuccess = save(optPatrolRecord);

//        //插入流程表记录——任务确认
//        OptHandleFlowRecord optHandleFlowRecord = new OptHandleFlowRecord();
//        optHandleFlowRecord.setTaskId(optPatrolRecord.getTaskId());//当前任务Id
//        //optHandleFlowRecord.setBusinessId(optPatrolRecord.getId());
//        optHandleFlowRecord.setActivityName("确认");
//        optHandleFlowRecord.setActivityFlag(3L);
//        //optHandleFlowRecord.setHandleUserId(SecurityUtils.getUserId());//当前登录用户Id
//        //optHandleFlowRecord.setHandleUserName(SecurityUtils.getUsername());//当前登录用户姓名
//        optHandleFlowRecord.setHandleTime(new Date());
//        optHandleFlowRecord.setCreateTime(new Date());
//        //optHandleFlowRecord.setAssignContent("已确认，处置中");
//        optHandleFlowRecord.setContent("新增巡查记录,已确认");
//        //optHandleFlowRecord.setCreateBy(SecurityUtils.getUsername());
//        optHandleFlowRecordService.save(optHandleFlowRecord);

        //插入流程表记录——任务处置中
        OptHandleFlowRecord optHandleFlowRecordHandle = new OptHandleFlowRecord();
        optHandleFlowRecordHandle.setTaskId(optPatrolRecord.getTaskId());//当前任务Id
        //optHandleFlowRecordHandle.setBusinessId(optPatrolRecord.getId());
        optHandleFlowRecordHandle.setActivityName("处置中");
        optHandleFlowRecordHandle.setActivityFlag(4L);
        optHandleFlowRecordHandle.setHandleUserId(SecurityUtils.getUserId());//当前登录用户Id
        optHandleFlowRecordHandle.setHandleUserName(SecurityUtils.getUsername());//当前登录用户姓名
        optHandleFlowRecordHandle.setHandleTime(new Date());
        optHandleFlowRecordHandle.setCreateTime(new Date());
        //optHandleFlowRecordHandle.setAssignContent("处置中");
        optHandleFlowRecordHandle.setContent("新增巡查记录,正在处置中");
        optHandleFlowRecordHandle.setCreateBy(SecurityUtils.getUsername());
        optHandleFlowRecordService.save(optHandleFlowRecordHandle);

        OptPatrolTask task = optPatrolTaskService.getById(optPatrolRecord.getTaskId());
        task.setStatusId(4L);
        task.setStatusName("处置中");
        return isSuccess && optPatrolTaskService.updateById(task);
    }
}
